# remoting.py - Jukee
#
# Copyright (C) 2008 Ruben Medellin <ruben.medellin.c@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the
#    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#    Boston, MA  02111-1307  USA

"""
Contains the initializers for the services. This is: the name server,
the even listener server and the routine that creates the Jukee Server
(which is the remote object the clients will be using)
"""

import Pyro.naming
import Pyro.EventService.Server
from threading import Thread
import time

from jukee.server.jukebox import JukeeServer
from jukee.utils.utils import debug, warn, error

#################### Name Server ####################

class NameServer(Thread):
  """Sets up the Pyro Name Server"""

  def __init__(self):
    Thread.__init__(self)
    self.setDaemon(True)
    self.starter = Pyro.naming.NameServerStarter()

  def run(self):
    debug("Launching Pyro Name Server")
    self.starter.start(hostname = Pyro.config.PYRO_NS_HOSTNAME)

  def waitUntilStarted(self):
    return self.starter.waitUntilStarted()

#################### Event Listener Server ####################

class EventServer(Thread):
  """Sets up the Pyro Event Listener Server"""

  def __init__(self):
    Thread.__init__(self)
    self.setDaemon(1)
    self.starter = Pyro.EventService.Server.EventServiceStarter()
    
  def run(self):
    debug("Launching Pyro Event Server")
    self.starter.start(hostname = Pyro.config.PYRO_NS_HOSTNAME)

  def waitUntilStarted(self):
    return self.starter.waitUntilStarted()

#################### MAIN INITIALIZATION ####################

def startServices():
  """
  Sets up the Pyro servers configuration and starts the name .ready
  server and the event server in their respective daemons
  """

  # Check for multithreading support
  if not Pyro.config.PYRO_MULTITHREADED:
    error("This program requires multithreading support")

  # Setup the configuration parameters
  Pyro.config.PYRO_NS_HOSTNAME = "192.168.1.145" #getHostname() ## FIXME
  warn("Pyro.config.PYRO_NS_HOSTNAME hardcoded into " + Pyro.config.PYRO_NS_HOSTNAME)

  # Create the servers and start them. Do not return until they are started
  name_server = NameServer()
  name_server.start()
  name_server.waitUntilStarted()

  event_server = EventServer()
  event_server.start()
  event_server.waitUntilStarted()

  # FIXME
  jukee_server = JukeeServer("test_jukebox", "This is a test jukebox for demonstration purposes")
  jukee_server.start()

  while not jukee_server.ready:
    time.sleep(1)
  debug("Server ready")

  return name_server, event_server, jukee_server
